package com.zzyl.nursing.controller.member;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.CountByTimeDto;
import com.zzyl.nursing.service.ReservationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/member/reservation")
public class ReservationController extends BaseController {

    @Autowired
    private ReservationService reservationService;

    @GetMapping("/cancelled-count")
    public AjaxResult getCancelledCount(){
        int result =reservationService.getCancelledCount();
        return AjaxResult.success(result);
    }

  @GetMapping("/countByTime")
    public AjaxResult countReservationsForTime(Long time){
      List<CountByTimeDto>  countByTimeDtos =reservationService.countReservationsForTime(time);
    return AjaxResult.success(countByTimeDtos);
  }


    @PostMapping
   public AjaxResult add(@RequestBody Reservation reservation){
        reservationService.add(reservation);
        return AjaxResult.success();
    }

    @GetMapping("/page")
    public AjaxResult page(Integer pageNum,Integer pageSize,Integer status){
        Reservation reservation = new Reservation();
        Map<String,Object> params = new HashMap<>();
        params.put("pageNum",pageNum);
        params.put("pageSize",pageSize);
        reservation.setParams(params);
        startPage();
        Long id=UserThreadLocal.getUserId();
        reservation.setCreateBy(String.valueOf(id));
        reservation.setStatus(status);
        List<Reservation> list = reservationService.selectReservationList(reservation);
        if (CollUtil.isEmpty(list)){
            throw new BaseException("无预约");
        }
        TableDataInfo<Reservation> tableDataInfo = getDataTable(list);
      return success(tableDataInfo);
    }

    @PutMapping("/{id}/cancel")
    public AjaxResult cancel(@PathVariable Long id){
        reservationService.updateStatus(id);
        return success();
    }
}
